/*
 *  $Id: DirectConnect.cs 1247 2011-03-23 14:26:34Z thenn.erannor $
 *	This file is a part of Ragima CCG.
 *	(C) 2008-2009 Ragima development group.
 *
 *	Licensed under the terms of the GNU GPL License version 2.
 *
 */

using System.Collections.Generic;

using AgateLib.DisplayLib;
using AgateLib.Geometry;

namespace Ragima {
    public class DirectConnectScene: OldScene {
        private Region[] _stdRegions;
        private Log _log;
        private UI.OldText _labelIP, _editIP;
        private UI.Button _buttonConnect, _buttonCreate, _buttonAccept, _buttonReject;
        private bool _needAccept, _myComplete, _remoteComplete;
        private int _startCounter, _startFrameCounter;

        private DirectConnectGameMode _mode;

        public DirectConnectScene() : base() {
            _log = new Log(new Rectangle(2, 228, 1020, 540), Data.Fonts.Log);
            _labelIP = new UI.OldText(new Rectangle(340, 10, 150, 20), Resources.DirectConnect.AddressLabel.GetText(), Color.Black);
            _editIP = new UI.OldText(new Rectangle(500, 10, 200, 20), Application.Config.Connection.DirectIPAddress, Color.Brown);
            _buttonConnect = new UI.ButtonNHPSprite(Data.ConnectButtons.GetSprite("connect"), new Point(413, 193), DoConnect);
            _buttonCreate = new UI.ButtonNHPSprite(Data.ConnectButtons.GetSprite("create"), new Point(506, 193), DoCreate);
            _buttonAccept = new UI.ButtonNHSprite(Data.ConnectButtons.GetSprite("accept"), new Point(340, 80), DoAccept);
            _buttonReject = new UI.ButtonNHSprite(Data.ConnectButtons.GetSprite("reject"), new Point(400, 80), DoReject);
            _stdRegions = new Region[] { _labelIP, _editIP, _buttonConnect, _buttonCreate, _log };

            _mode = new DirectConnectGameMode();

            _needAccept = false;

            UpdateRegions();
        }

        public Log Log {
            get { return _log; }
        }

        public DirectConnectGameMode Mode {
            get { return _mode; }
        }

        public override void FinalizeScene() {
            base.FinalizeScene();
            //_mode.CancelCreateGame();
            //_mode.DisconnectGame();
        }

        protected override void UpdateRegions(List<Region> regions) {
            regions.AddRange(_stdRegions);
            if (_needAccept) {
                regions.Add(_buttonAccept);
                regions.Add(_buttonReject);
            }
        }

        public override void Draw() {
            Data.BackgroundBlank.Draw();
            Data.ConnectTop.Draw();
            DrawRegions();
            DrawHint();
        }

        public override void Process() {
            _mode.ProcessEvents(this);
            base.Process();
            ProcessStartCounter();
        }

        private void DoConnect() {
            UnsetComplete();
            string addr = Application.Config.Connection.DirectIPAddress;
            _log.Add(Resources.DirectConnect.TryConnect, addr);
            EnableConnectButtons(false);
            _mode.ConnectToGame(addr);
        }

        private void DoCreate() {
            UnsetComplete();
            if (_mode.CreateGame()) {
                EnableConnectButtons(false);
                _log.Add(Resources.DirectConnect.WaitConnect);
            } else {
                _log.Add(Resources.DirectConnect.WaitConnectError);
            }
        }

        private void DoAccept() {
            DoAccept(true);
        }
        private void DoReject() {
            DoAccept(false);
        }
        private void DoAccept(bool accept) {
            EnableAcceptButtons(false);
            _mode.AddEvent(new Events.Accept(accept));
        }

        public void EnableConnectButtons(bool enabled) {
            _buttonConnect.Enabled = enabled;
            _buttonCreate.Enabled = enabled;
        }

        public void EnableAcceptButtons(bool enabled) {
            if (enabled) {
                Point pos = _log.GetLastLinePosition();
                pos.X += _log.GetLineWidth(_log.LineCount-1) + 4;
                pos.Y += 1;
                _buttonAccept.Rect = new Rectangle(pos, _buttonAccept.Rect.Size);
                pos.X += _buttonAccept.Rect.Width + 4;
                _buttonReject.Rect = new Rectangle(pos, _buttonReject.Rect.Size);
            }
            _needAccept = enabled;
            UpdateRegions();
        }

        public void CheckDeckRequest() {
            _mode.SendEvent(new Events.CheckDeck(_mode.Player1.Deck));
        }

        private void UnsetComplete() {
            _myComplete = false;
            _remoteComplete = false;
        }

        private bool IsComplete {
            get { return _myComplete && _remoteComplete; }
        }

        public void SetComplete(bool remote) {
            if (remote)
                _remoteComplete = true;
            else
                _myComplete = true;
            if (IsComplete) {
                _log.Add(Resources.DirectConnect.DataExchangeComplete);
                _startCounter = 5;
                _startFrameCounter = 0;
            }
        }

        private void ProcessStartCounter() {
            if (!IsComplete || _startCounter < 0)
                return;
            if (_startFrameCounter > 0) {
                _startFrameCounter--;
                return;
            }
            if (_startCounter > 0) {
                _log.Add(Resources.DirectConnect.StartCounter, _startCounter);
                _startCounter--;
                _startFrameCounter = 50;
                return;
            }
            _startCounter = -1;
            _log.Add(Resources.DirectConnect.StartGame);
            if (_mode.Master)
                _mode.AddEvent(new Events.StartGame());
        }
    }
}
